﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAtletIS.Models;

namespace MvcAtletIS.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class DisciplineListController : Controller
    {
        private atletisEntities db = new atletisEntities();

        //    

        // GET: /DisciplineList/

        public ViewResult Index(int competitionId)
        {
            var disciplinelist = db.DisciplineList
                .Where(d => d.CompetitionId == competitionId)
                .OrderBy(d => d.ExternalId)
                .Include("Categories")
                .Include("Competitions")
                .Include("Disciplines");
            
            ViewBag.Competition = db.Competitions.Single(c => c.ID == competitionId);
            return View(disciplinelist.ToList());
        }

        //
        // GET: /DisciplineList/Details/5

        public ViewResult Details(int id)
        {
            DisciplineList disciplinelist = db.DisciplineList.Single(d => d.ID == id);
            ViewBag.Competition = db.Competitions.Single(c => c.ID == disciplinelist.CompetitionId);
            return View(disciplinelist);
        }

        //
        // GET: /DisciplineList/Create

        public ActionResult Create(int competitionId)
        {
            var categories = db.Categories.Where(c => c.Active == true);
            var disciplines = db.Disciplines.Where(d => d.Active == true);
            
            ViewBag.CategoryId = new SelectList(categories, "ID", "Name");
            ViewBag.DisciplineId = new SelectList(disciplines, "ID", "Name");

            ViewBag.Competition = db.Competitions.Single(c => c.ID == competitionId);

            DisciplineList model = new DisciplineList();
            model.CompetitionId = competitionId;
            model.Day = 1;

            return View(model);
        } 

        //
        // POST: /DisciplineList/Create

        [HttpPost]
        public ActionResult Create(DisciplineList model)
        {
            var discipline = db.Disciplines.Single(d => d.ID == model.DisciplineId);
            var category = db.Categories.Single(c => c.ID == model.CategoryId);
            
            model.Name = discipline.Name + " " + category.Name;
            model.ParentId = 0;
            model.Status = 1;
            model.DisciplineCode = discipline.Code;
            model.InnerCount = 0;
            model.Sync = true;
            model.Active = true;
            model.MeasurementId = 1;

            if (ModelState.IsValid)
            {
                db.DisciplineList.AddObject(model);
                db.SaveChanges();
                return RedirectToAction("Index", new {competitionId = model.CompetitionId });  
            }

            ViewBag.CategoryId = new SelectList(db.Categories, "ID", "Name", model.CategoryId);
            ViewBag.CompetitionId = new SelectList(db.Competitions, "ID", "Name", model.CompetitionId);
            ViewBag.DisciplineId = new SelectList(db.Disciplines, "ID", "Name", model.DisciplineId);

            return View(model);
        }
        
        //
        // GET: /DisciplineList/Edit/5
 
        public ActionResult Edit(int id)
        {
            DisciplineList disciplinelist = db.DisciplineList.Single(d => d.ID == id);

            if (disciplinelist == null)
            {
                return HttpNotFound();
            }

            ViewBag.Competition = db.Competitions.Single(c => c.ID == disciplinelist.CompetitionId);
            ViewBag.Category = db.Categories.Single(c => c.ID == disciplinelist.CategoryId);
            ViewBag.Discipline = db.Disciplines.Single(d => d.ID == disciplinelist.DisciplineId);
            
            return View(disciplinelist);
        }

        //
        // POST: /DisciplineList/Edit/5

        [HttpPost]
        public ActionResult Edit(DisciplineList disciplinelist)
        {
            if (String.IsNullOrEmpty(disciplinelist.Description))
            {
                disciplinelist.Description = string.Empty;
            }

            if (String.IsNullOrEmpty(disciplinelist.Note))
            {
                disciplinelist.Note = string.Empty;
            }
            
            disciplinelist.Modified = DateTime.Now;
            
            if (ModelState.IsValid)
            {
                db.DisciplineList.Attach(disciplinelist);
                db.ObjectStateManager.ChangeObjectState(disciplinelist, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index", new { competitionId = disciplinelist.CompetitionId });
            }
            ViewBag.CategoryId = new SelectList(db.Categories, "ID", "Name", disciplinelist.CategoryId);
            ViewBag.CompetitionId = new SelectList(db.Competitions, "ID", "Name", disciplinelist.CompetitionId);
            ViewBag.DisciplineId = new SelectList(db.Disciplines, "ID", "Name", disciplinelist.DisciplineId);
            return View(disciplinelist);
        }

        //
        // GET: /DisciplineList/Delete/5
 
        public ActionResult Delete(int id)
        {
            DisciplineList disciplinelist = db.DisciplineList.Single(d => d.ID == id);
            ViewBag.Competition = db.Competitions.Single(c => c.ID == disciplinelist.CompetitionId);
            return View(disciplinelist);
        }

        //
        // POST: /DisciplineList/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                DisciplineList disciplinelist = db.DisciplineList.Single(d => d.ID == id);

                if (disciplinelist == null)
                {
                    return HttpNotFound();
                }
                
                int? competitionId = disciplinelist.CompetitionId;
                db.DisciplineList.DeleteObject(disciplinelist);
                db.SaveChanges();

                return RedirectToAction("Index", new { competitionId = competitionId });
            }
            catch (Exception ex)
            {
                return RedirectToAction("Error", "Home", new { message = ex.Message + " " + ex.InnerException.ToString().Substring(0, 1000) });
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}